package com.youzhi.backend.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youzhi.backend.dto.TreeNode;
import com.youzhi.backend.entity.Button;
import com.youzhi.backend.entity.Menu;
import com.youzhi.backend.mapper.MenuMapper;
import com.youzhi.backend.service.IButtonService;
import com.youzhi.backend.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统菜单表 服务实现类
 * </p>
 *
 * @author xumeng
 * @since 2022-12-02
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {


    @Autowired
    private IButtonService service;

    public void setChildren(TreeNode node) {
        List<TreeNode> list = getByParentId(node.getId());
        //没有儿子节点，证明是叶子节点，需要给叶子节点绑定按钮
        if (CollUtil.isEmpty(list)) {
            bindButtons(node);
            return;
        }
        for (TreeNode treeNode : list) {
            setChildren(treeNode);
        }
        node.setChildren(list);
    }


    private void bindButtons(TreeNode node) {
        List<Button> buttons = service.getByMenuId(node.getId());
        if (CollUtil.isNotEmpty(buttons)) {
            List<TreeNode> treeNodes = buttonConvertTreeNode(buttons);
            node.setChildren(treeNodes);
        }
    }


    private List<TreeNode> getByParentId(Long id) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Menu.Fields.parentId, id);
        List<Menu> menus = list(queryWrapper);

        if (CollUtil.isNotEmpty(menus)) {
            return menuConvertTreeNode(menus);
        } else {
            return null;
        }
    }

    private List<TreeNode> menuConvertTreeNode(List<Menu> menus) {
        return menus.stream().map(menu -> {
            TreeNode treeNode = new TreeNode();
            treeNode.setId(menu.getId());
            treeNode.setLabel(menu.getName());
            treeNode.setCode(menu.getPrivilegeCode());
            return treeNode;
        }).collect(Collectors.toList());
    }


    private List<TreeNode> buttonConvertTreeNode(List<Button> buttons) {
        return buttons.stream().map(button -> {
            TreeNode treeNode = new TreeNode();
            treeNode.setId(button.getId());
            treeNode.setCode(button.getPrivilegeCode());
            treeNode.setLabel(button.getName());
            return treeNode;
        }).collect(Collectors.toList());
    }

    /**
     * 获取树形结构
     *
     * @return
     */
    public TreeNode tree() {
        TreeNode root = new TreeNode();
        root.setLabel("根目录");
        root.setId(0L);
        setChildren(root);
        return root;
    }


    public Menu getByName(String name) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq(Menu.Fields.name, name);
        return getOne(wrapper);
    }

}
